﻿#ifndef _DATABASE_DATASOURCE_H_H_
#define _DATABASE_DATASOURCE_H_H_

#include "udatabase/dll_cfg.h"
#include "udatabase/sqldatabase.h"
#include "kernel/unicodestring.h"
#include "udatabase/interface/idatasource.h"


namespace FD{
	namespace Utility{
		namespace DB{

				class CSqlstandardization;
				///<summary>
				///根据给定连接创建数据源
				///</summary>
				class FD_DB_Export CDatabaseDataSource : public FD::Utility::DB::Interface::IDataSource
				{
				public:
					///<summary>
					///析构函数
					///</summary>
					virtual ~CDatabaseDataSource();
					///<summary>
					///获取数据库类型
					/// </summary>
					/// <param name=""></param>
					///<returns>数据库类型</returns>
					const FD::Kernel::FdUnicodeString& getDatabaseType() const;
					///<summary>
					///获取驱动类型类型
					/// </summary>
					/// <param name=""></param>
					///<returns>驱动类型</returns>
					FD::Kernel::FdUnicodeString getDriver() const;
					///<summary>
					///获取数据库名
					/// </summary>
					/// <param name=""></param>
					///<returns>数据库名</returns>
					FD::Kernel::FdUnicodeString getDatabaseName() const;
					///<summary>
					///获取主机名
					/// </summary>
					/// <param name=""></param>
					///<returns>主机名</returns>
					FD::Kernel::FdUnicodeString getHostName() const;
					///<summary>
					///获取端口号
					/// </summary>
					/// <param name=""></param>
					///<returns>端口号</returns>
					INT32 getPort() const;
					///<summary>
					///获取用户名
					/// </summary>
					/// <param name=""></param>
					///<returns>用户名</returns>
					FD::Kernel::FdUnicodeString getUserName() const;
					///<summary>
					///获取密码
					/// </summary>
					/// <param name=""></param>
					///<returns>密码</returns>
					FD::Kernel::FdUnicodeString getPassword() const;
					///<summary>
					///获取连接选项
					/// </summary>
					/// <param name=""></param>
					///<returns>连接选项</returns>
					FD::Kernel::FdUnicodeString getConnectOptions() const;
				public:
					///<summary>
					///初始化数据源
					/// </summary>
					/// <param name=""></param>
					///<returns>成功返回0，否则返回-2</returns>
					virtual INT32 initialize();
				protected:
					INT32 createTableInfoTable();					
					INT32 createSpatialRefSysTable();
					INT32 createSpatialIndexTable();
					INT32 createGeometryColumnsTable();
					INT32 createDatasetInfoTable();
					INT32 createFieldInfoTable();
					INT32 createFeatureClassesInfo();
					INT32 createVectorDatasetTable();
					INT32 createDataDictionaryTable();
					INT32 createDatasetLockTable();
					INT32 createTable(const FD::Kernel::FdUnicodeString& sql);
					
				public:
					///<summary>
					///获取数据集别名
					/// </summary>
					/// <param name="datasetName">数据集名</param>
					///<returns>根据要素集名称返回相应的八位字符串,若要素不存在，返回""</returns>
					String getDatasetAliasName (const String& datasetName);
					///<summary>
					///获取数据集别名
					/// </summary>
					/// <param name="datasetID">数据集id号</param>
					///<returns>别名</returns>
					String getDatasetAliasName (UINT32 datasetID);		

					virtual FD::Utility::DB::CSqlDatabase& getDatabase();

				protected:
					CDatabaseDataSource(const FD::Utility::DB::CSqlDatabase &db);
					CDatabaseDataSource(const FD::Utility::DB::CSqlDatabase &db, const String& dbType);

					FD::Utility::DB::CSqlDatabase		m_ddb;

					FD::Kernel::RecursiveMutex	mThreadMutex;
					map<uint32, FD::Utility::DB::CSqlDatabase> mThreadDDB;

					FD::Kernel::FdUnicodeString			m_dbType;
					CSqlstandardization *sqlStd;
				};

			}}}// End of Namespace



#endif //_DATABASE_DATASOURCE_H_H_

